package com.sgq.vue.service.resource.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgq.vue.dao.resource.WeatherApiMapper;
import com.sgq.vue.entity.resource.City;
import com.sgq.vue.entity.resource.Weather;
import com.sgq.vue.entity.resource.WeatherApi;
import com.sgq.vue.enums.RequestConditionEnums;
import com.sgq.vue.service.resource.WeatherApiService;
import com.sgq.vue.utils.resource.WeatherUtil;
import com.sgq.vue.vo.query.resource.WeatherApiQueryVo;
import com.sgq.vue.vo.query.resource.WeatherRequestQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: 83778
 * @date: 2023-12-26 0:40
 * @desc:
 */
@Slf4j
@Service
public class WeatherApiServiceImpl extends ServiceImpl<WeatherApiMapper, WeatherApi> implements WeatherApiService {
    @Override
    public  IPage<WeatherApi>  findWeatherApiByPage(WeatherApiQueryVo weatherApiQueryVo) {
        Long pageNo = weatherApiQueryVo.getPageNo();
        Long pageSize = weatherApiQueryVo.getPageSize();
        IPage<WeatherApi> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<WeatherApi> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(weatherApiQueryVo.getApiName()),WeatherApi::getApiName,weatherApiQueryVo.getApiName());
        IPage<WeatherApi> selectPage = this.baseMapper.selectPage(page, queryWrapper);
        return selectPage;
    }

    @Override
    @CacheEvict(cacheNames = "weather::user" , key = "'userId:'+ #userId",allEntries = true)
    public boolean updateState(Long id , Integer state , Long userId){
        Integer update =  this.baseMapper.updateState(id , state);
        return update == 1 ? true : false;
    }

    /**
     *
     * @param belongs 同等于 UserId
     * @return
     */
    @Override
    public List<WeatherApi> findWeatherApiByBelongs(Long belongs) {
        LambdaQueryWrapper<WeatherApi> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WeatherApi::getBelongs,belongs);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public String sendRequest(WeatherRequestQueryVo weatherRequestQueryVo) {
        System.out.println("weatherRequestVo :"+ weatherRequestQueryVo);
        String requestUrl = weatherRequestQueryVo.getRequestUrl();
        Integer typeId = weatherRequestQueryVo.getTypeId();
        City city = weatherRequestQueryVo.getCity();
        //获取conditionKey
        WeatherApi weatherApi = this.baseMapper.selectById(weatherRequestQueryVo.getId());
        String conditionKey = weatherApi.getConditionKey();
        //type决定key是哈
        String conditionType = weatherApi.getConditionType();

        String[] typeArr = conditionType.split(",");

        List<Integer> typeIdList = Arrays.stream(typeArr)
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        /**
         *  url :  https://api.seniverse.com/v3/weather/now.json?key=&value=&
         *  condition_key : [location,location]
         *  condition_type : [0,2]
         *  记录index，用于知道是哪个 key
         */
        int indexOfConditionKey = typeIdList.indexOf(typeId);

        //拿到对应的 conditionKey
        conditionKey = conditionKey.split(",")[indexOfConditionKey];
        requestUrl = requestUrl +"&"+conditionKey + "=" ;
        if (typeId.equals(RequestConditionEnums.CITY_CODE.getTypeId())){
            requestUrl += city.getId();
        }else if (typeId.equals(RequestConditionEnums.CITY_EN.getTypeId())){
            requestUrl += city.getCityEn();
        }else{
            requestUrl += city.getCityZh();
        }
        String json = WeatherUtil.sendRequest(requestUrl);
        System.out.println("json = " + json);
        return json;
    }
}
